package com.hqhsc.module.manage.system.controller;

import com.hqhsc.module.common.domain.Menu;
import com.hqhsc.module.manage.system.domain.Org;
import com.hqhsc.module.common.domain.Role;
import com.hqhsc.module.common.service.CommonService;
import com.hqhsc.module.init.service.InitService;
import com.hqhsc.module.login.domain.User;
import com.hqhsc.module.manage.system.service.SystemManageService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Properties;

@Controller
@RequestMapping("/manage/system")
public class SystemManageController {
	final Logger logger = LoggerFactory.getLogger(SystemManageController.class);

	@Autowired
	private CommonService commonService;
    @Autowired
    private SystemManageService systemManageService;
    @Autowired
    private InitService initService;
	@Autowired
	private Properties appProps;


    /**
     * 系统管理-人员管理
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/user/index" , method = {RequestMethod.GET , RequestMethod.POST})
    public String userindex(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        String pageindexStr = request.getParameter("page_index");//第几页
        int page_index = Integer.parseInt(StringUtils.isBlank(pageindexStr) || "0".equals(pageindexStr) ? "1" : pageindexStr);
        int size = Integer.valueOf(appProps.get("manage.systemuser.index.size").toString());//每页显示条数
        int start = (page_index - 1) * size;

        String user_name = request.getParameter("username");

        String method = request.getMethod();
        if("GET".equals(method.toUpperCase()) && StringUtils.isNotEmpty(user_name)) {//get请求，进行编码格式转换
            user_name = this.commonService.characterFormat(user_name , "ISO8859-1" , "UTF-8");
        }

        Map<String , Object> map = this.systemManageService.getUserList(0, user_name, start, size);
        long total = (Long)map.get("total");;
        List<User> user_list = (List<User>)map.get("user_list");

        long temp = (total - 1) <= 0 ? 0 : (total - 1);
        int pages = Integer.parseInt(Long.toString(temp / size)) + 1;
        int prepages = (page_index - 1) <= 0 ? 1 : (page_index - 1);
        int nextpages = (page_index + 1) >= pages ? pages : (page_index + 1);

        model.addAttribute("current_page" , page_index);
        model.addAttribute("pages" , pages);
        model.addAttribute("prepage" , prepages);
        model.addAttribute("nextpage" , nextpages);
        model.addAttribute("page_url" , request.getRequestURI());

        String condition = "";
        if(StringUtils.isNotEmpty(user_name)) {
            condition = condition + "&username="+user_name;
        }
        model.addAttribute("condition" , condition);

        model.addAttribute("user_name" , user_name);
        model.addAttribute("user_list" , user_list);
        return "/module/manage/system/user/index";
    }

    /**
     * 系统管理-人员管理-新增
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/user/add" , method = RequestMethod.GET)
    public String userAddIndex(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        List<Role> roleList = this.commonService.getUserCanUseRole(user.getRole_list().get(0).getRole_id());
        model.addAttribute("role_list" , roleList);
        return "/module/manage/system/user/add";
    }

    /**
     * 系统管理-人员管理-新增 保存
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/user/doadd" , method = RequestMethod.POST)
    @ResponseBody
    public int userDoAdd(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        String login_name = request.getParameter("login_name");
        String login_pwd = request.getParameter("login_pwd");
        String user_name = request.getParameter("user_name");
        String nick_name = request.getParameter("nick_name");
        String employee_id = request.getParameter("employee_id");

//        String org_id = request.getParameter("org_id");
        String org_id = "1";
        String role_id = request.getParameter("role_id");

        return this.systemManageService.userDoAdd(user.getUser_id(), login_name, login_pwd, user_name, nick_name , org_id , role_id , employee_id);
    }

    /**
     * 系统管理-人员管理-编辑
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/user/edit" , method = RequestMethod.GET)
    public String peopleEditIndex(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        String edited_user_id = request.getParameter("edited_user_id");//被编辑的用户id
        User edited_user = commonService.getUserById(Long.valueOf(edited_user_id));
        model.addAttribute("edited_user" , edited_user);

        return "/module/manage/system/user/edit";
    }

    /**
     * 系统管理-人员管理-编辑 保存
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/user/doedit" , method = RequestMethod.POST)
    @ResponseBody
    public int userDoEdit(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        String edited_user_id = request.getParameter("edited_user_id");//被编辑的用户id
        String real_name = request.getParameter("user_name");
        String employee_id = request.getParameter("employee_id");
        String nice_name = request.getParameter("nice_name");

        return this.systemManageService.userDoEdit(user.getUser_id() , real_name , Long.valueOf(edited_user_id) , employee_id , nice_name);
    }

    /**
     * 执行删除操作
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/user/dodelete" , method = RequestMethod.POST)
    @ResponseBody
    public int userDoDelete(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        String edited_user_id = request.getParameter("edited_user_id");//被编辑的用户id
        return this.systemManageService.userDoDelete(user.getUser_id() , Long.valueOf(edited_user_id));
    }

    /**
     * 系统管理-角色管理
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/role/index" , method = {RequestMethod.GET , RequestMethod.POST})
    public String roleindex(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        List<Role> role_list = this.initService.getSysRole();
        model.addAttribute("role_list" , role_list);
        return "/module/manage/system/role/index";
    }


    /**
     * 系统管理-组织管理
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/org/index" , method = {RequestMethod.GET , RequestMethod.POST})
    public String orgIndex(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");


        String pageindexStr = request.getParameter("page_index");//第几页
        int page_index = Integer.parseInt(StringUtils.isBlank(pageindexStr) || "0".equals(pageindexStr) ? "1" : pageindexStr);
        int size = Integer.valueOf(appProps.get("manage.org.index.size").toString());//每页显示条数
        int start = (page_index - 1) * size;

        String org_name = request.getParameter("org_name");

        String method = request.getMethod();
        if("GET".equals(method.toUpperCase())) {//get请求，进行编码格式转换
            org_name = this.commonService.characterFormat(org_name , "ISO8859-1" , "UTF-8");
        }

        Map<String , Object> map = this.systemManageService.getOrgList(org_name, start, size);
        long total = (Long)map.get("total");
        List<Org> org_list = (List<Org>)map.get("org_list");

        long temp = (total - 1) <= 0 ? 0 : (total - 1);
        int pages = Integer.parseInt(Long.toString(temp / size)) + 1;
        int prepages = (page_index - 1) <= 0 ? 1 : (page_index - 1);
        int nextpages = (page_index + 1) >= pages ? pages : (page_index + 1);

        model.addAttribute("current_page" , page_index);
        model.addAttribute("pages" , pages);
        model.addAttribute("prepage" , prepages);
        model.addAttribute("nextpage" , nextpages);
        model.addAttribute("page_url" , request.getRequestURI());

        String condition = "";
        if(StringUtils.isNotEmpty(org_name)) {
            condition = condition + "&org_name="+org_name;
        }
        model.addAttribute("condition" , condition);

        model.addAttribute("org_name" , org_name);
        model.addAttribute("org_list" , org_list);
        return "/module/manage/system/org/index";
    }



    /**
     * 系统管理-人员管理-新增
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/org/add" , method = RequestMethod.GET)
    public String orgAddIndex(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        return "/module/manage/system/org/add";
    }

    /**
     * 系统管理-人员管理-新增 保存
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/org/doadd" , method = RequestMethod.POST)
    @ResponseBody
    public int orgDoAdd(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        String org_name = request.getParameter("org_name");
        String org_address = request.getParameter("org_address");

        return this.systemManageService.orgDoAdd(org_name , org_address , user.getUser_id());
    }

    /**
     *
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/rolemenu/index" , method = {RequestMethod.GET , RequestMethod.POST})
    public String roleMenuIndex(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        String choose_role_id = request.getParameter("choose_role_id");
        long role_id = (choose_role_id == null || "".equals(choose_role_id)) ? 20000:Long.valueOf(choose_role_id);
        List<Role> role_list = this.initService.getSysRole();

        //获取传入角色对应的菜单，如果role_id没有传，则使用默认值20000
        Map<String , List<Menu>> map = this.systemManageService.getRoleMenuList(role_id);

        int is_user_admin = (user.getRole_list().get(0).getRole_id() == 10000) ? 1 : 0;

        model.addAttribute("is_user_admin" , is_user_admin);
        model.addAttribute("choose_role_id" , role_id);
        model.addAttribute("role_list" , role_list);
        model.addAttribute("top_menu_list" , map.get("top_menu_list"));
        model.addAttribute("sub_menu_list" , map.get("sub_menu_list"));

        return "/module/manage/system/rolemenu/index";
    }

    @RequestMapping(value = "/rolemenu/doconfig" , method = RequestMethod.POST)
    @ResponseBody
    public int roleMenuDoConfig(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");
        String sub_menu_ids = request.getParameter("sub_menu_id");//权限菜单id，xxx,xxx,xxx格式
        long role_id = Long.valueOf(request.getParameter("role_id"));//角色id

        return this.systemManageService.roleMenuDoConfig(sub_menu_ids, role_id , user.getUser_id());
    }



    @RequestMapping(value = "/password/modify" , method = RequestMethod.GET)
    public String modifyPassword(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        return "/module/manage/system/password/modify";
    }

    @RequestMapping(value = "/password/domodify" , method = RequestMethod.POST)
    @ResponseBody
    public int doModifyPassword(Model model , HttpServletRequest request , HttpServletResponse response) {
        User user = (User)request.getSession().getAttribute("user");

        String old_password = request.getParameter("old_password");
        String new_password = request.getParameter("new_password");
        String confirm_password = request.getParameter("confirm_password");

        return this.systemManageService.doModifyPassword(old_password , new_password , confirm_password , user.getLogin_pwd() , user.getUser_id());
    }
     
}